﻿using GrainManage.Encrypt;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace GrainManage.Web.Common
{
    public class RsaCrypto
    {
        private static string exponent;
        private static string modulus;
        private static readonly object lockObj = new object();

        public static string RsaProvider
        {
            get
            {
                return GrainManage.Web.Resources.GlobalResx.PrivateKey;
            }
        }

        public static string Decrypt(string text)
        {
            return RSAEncrypt.Decrypt(text, RsaProvider);
        }

        public static string Encrypt(string text)
        {
            return RSAEncrypt.Encrypt(text, RsaProvider);
        }

        public static string Exponent
        {
            get
            {
                if (string.IsNullOrEmpty(exponent))
                {
                    lock (lockObj)
                    {
                        if (string.IsNullOrEmpty(exponent))
                        {
                            var para = RSAEncrypt.GetRsaParameters(RsaProvider);
                            exponent = FormatConvertor.ToHexString(para.Exponent);
                        }
                    }
                }
                return exponent;
            }
        }

        public static string Modulus
        {
            get
            {
                if (string.IsNullOrEmpty(modulus))
                {
                    lock (lockObj)
                    {
                        if (string.IsNullOrEmpty(modulus))
                        {
                            var para = RSAEncrypt.GetRsaParameters(RsaProvider);
                            modulus = FormatConvertor.ToHexString(para.Modulus);
                        }
                    }
                }
                return modulus;
            }
        }
    }
}